<?php
/* 使用方式
 * uvan_split_page(99999,$cur_page,20,'?ac=def&page={page}',''); {page} 将有程序替换
 * uvan_split_page(99999,$cur_page,20,'javascript:alert({page})',''); js 请求分页, 可以把alert换成你的回调函数
 */

/**
 * @param integer $total_num 总记录数
 * @param integer $cur_page  当前页数
 * @param integer $num_per_page 每页显示行数
 * @param string $url ＵＲＬ地址
 * @param string $total_str　总共多少篇文章【用户自定义显示文本, 不需要则传空】【实例：照片列表页可用(共{total_num}张照片)】
 * @param integer $mid_area_num　当前选项左右各几条记录
 * @return string
 */
function uvan_split_page($total_num, $cur_page=1, $num_per_page=20, $url='javascript:alert({page});', $total_str='', $mid_area_num = 3)
{
	//判断参数
	if($total_num <= 0)
	{
		return '';
	}

	$total_page = ceil($total_num / $num_per_page);
	
	//当只有一页时不显示本页的页码
	if( $total_page > 1 )
		$cur_href = '<li><span>{page}</span></li>';
	else
		$cur_href = '';

	$base_html = uvan_get_core_split_page($total_num,
							$cur_page,
							$num_per_page,
							$total_str,
							'<li><a href="'.$url.'">{page}</a></li>',
							$cur_href,
							'<li class="bold"><a href="'.$url.'">上一页</a></li>',
							'<li class="bold"><a href="'.$url.'">下一页</a></li>',
							'<li>...</li>',
							$mid_area_num
							);
	return "<ul>$base_html</ul>";
}

function uvan_get_core_split_page($total_num, 
								$cur_page = 1,
								$num_per_page = 20,
								$total_str = '',
								$page_str = '<li><a href="javascript:alert(\'跳到第{page}\')">[{page}]</a></li>',
								$cur_page_str = '<b>{page}</b>',
								$pre_page_str = '<li><a href="javascript:alert(\'跳到第{page}\')">前一页</a></li>',
								$next_page_str = '<li><a href="javascript:alert(\'跳到第{page}\')">后一页</a></li>',
								$ellipsis_str = '...',
								$mid_area_num = 2)
{
	$total_num = intval($total_num);
	$cur_page = intval($cur_page);
	$num_per_page = intval($num_per_page);
	
	//参数校验
	if( $total_num <= 0 )
	{
		return false;
	}
	$total_page = ceil($total_num / $num_per_page);
	if( $cur_page <= 0 )
		$cur_page = 1;
	if( $cur_page > $total_page )
		$cur_page = $total_page;
	
	$total_str = uvan_split_page_tpl_replace($total_str, 0, $total_num, $total_page);
	
	//第一页，不需要"前一页"
	if( $cur_page == 1 )
	{
		$pre_page_str = '';
		$first_page_str = '';
	}
	else 
	{
		$first_page_str = uvan_split_page_tpl_replace($page_str, 1, $total_num, $total_page);
		$pre_page = $cur_page - 1;
		$pre_page_str = uvan_split_page_tpl_replace($pre_page_str, $pre_page, $total_num, $total_page);
	}

	//最后一页，不需要"后一页"
	if( $cur_page == $total_page )
	{
		$next_page_str = '';
		$last_page_str = '';
	}
	else 
	{
		$next_page = $cur_page + 1;
		$next_page_str = uvan_split_page_tpl_replace($next_page_str, $next_page, $total_num, $total_page);
		$last_page_str = uvan_split_page_tpl_replace($page_str, $total_page, $total_num, $total_page);
	}
	
	$pre_str = '';
	//特殊需求，当省略号部分只有1个页码时，不显示... 直接显示页码
	if( $cur_page - 1 == $mid_area_num + 2 )
		$pre_str .= uvan_split_page_tpl_replace($page_str, 2, $total_num, $total_page);
	else if ($cur_page - 1 > $mid_area_num + 2 )
		$pre_str .= $ellipsis_str;

	for( $i = max($cur_page-$mid_area_num, 2); $i < $cur_page; $i++ )
	{
		$pre_str .= uvan_split_page_tpl_replace($page_str, $i, $total_num, $total_page);
	}
	
	$next_str = '';
	for( $i = $cur_page+1; $i < min($cur_page+$mid_area_num+1, $total_page); $i++ )
	{
		$next_str .= uvan_split_page_tpl_replace($page_str, $i, $total_num, $total_page);
	}
	//特殊需求，当省略号部分只有1个页码时，不显示... 直接显示页码
	if( $total_page - $cur_page == $mid_area_num + 2 )
		$next_str .= uvan_split_page_tpl_replace($page_str, $total_page-1, $total_num, $total_page);
	else if ( $total_page - $cur_page > $mid_area_num + 2 )
		$next_str .= $ellipsis_str;

	$cur_page_str = uvan_split_page_tpl_replace($cur_page_str, $cur_page, $total_num, $total_page);
	return "$total_str $pre_page_str $first_page_str $pre_str $cur_page_str $next_str $last_page_str $next_page_str";
}

function uvan_split_page_tpl_replace($str, $page, $total_num, $total_page)
{
	$tags = array('{page}', '{total_num}', '{total_page}');
	$replaced_tag = array($page, $total_num, $total_page);
	return str_ireplace($tags, $replaced_tag, $str);
}
?>